home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / OpenDoc 1.2b2c1 / Implementation / Memory / MemHook.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-13  |  8.5 KB  |  321 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        MemHook.cpp
  3.  
  4.     Contains:    MemoryHook base class implementation
  5.  
  6.     Owned by:    Jens Alfke
  7.  
  8.     Copyright:    © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.     
  12.          <1>     9/13/96    jpa        first checked in
  13.  
  14.     To Do:
  15.     In Progress:
  16.         
  17. */
  18.  
  19. #ifndef _PLATFMEM_
  20. #include "PlatfMem.h"
  21. #endif
  22.  
  23. #ifndef _MEMHOOK_
  24. #include "MemHook.h"
  25. #endif
  26.  
  27. #ifndef __MEMORY__
  28. //#include <Memory.h>
  29. #endif
  30.  
  31. #ifndef __STDIO__
  32. //#include <stdio.h>
  33. #endif
  34.  
  35.  
  36. //========================================================================================
  37. // CLASS ODMemoryHook (MM_DEBUG only)
  38. //========================================================================================
  39.  
  40. #if MM_DEBUG
  41. //----------------------------------------------------------------------------------------
  42. // ODMemoryHook::ODMemoryHook
  43. //----------------------------------------------------------------------------------------
  44. #pragma segment HeapSeg
  45.  
  46. ODMemoryHook::ODMemoryHook()
  47. {
  48.     fNextHook = fPreviousHook = this;
  49. }
  50. #endif
  51.  
  52. #if MM_DEBUG
  53. //----------------------------------------------------------------------------------------
  54. // ODMemoryHook::~ODMemoryHook
  55. //----------------------------------------------------------------------------------------
  56. #pragma segment HeapSeg
  57.  
  58. ODMemoryHook::~ODMemoryHook()
  59. {
  60. }
  61. #endif
  62.  
  63. #if MM_DEBUG
  64. //----------------------------------------------------------------------------------------
  65. // ODMemoryHook::operator new
  66. //----------------------------------------------------------------------------------------
  67. #pragma segment HeapSeg
  68.  
  69. void* ODMemoryHook::operator new(SIZE_T size, MMHeapLocation src)
  70. {
  71.     return PlatformAllocateBlock(size, src);
  72. }
  73. #endif
  74.  
  75. #if MM_DEBUG
  76. //----------------------------------------------------------------------------------------
  77. // ODMemoryHook::operator delete
  78. //----------------------------------------------------------------------------------------
  79. #pragma segment HeapSeg
  80.  
  81. void ODMemoryHook::operator delete(void* ptr)
  82. {
  83.         PlatformFreeBlock(ptr);
  84. }
  85. #endif
  86.  
  87. #if MM_DEBUG
  88. //----------------------------------------------------------------------------------------
  89. // ODMemoryHook::GetHeaderSize
  90. //----------------------------------------------------------------------------------------
  91. #pragma segment HeapSeg
  92.  
  93. ODBlockSize ODMemoryHook::GetHeaderSize()
  94. {
  95.     return 0;
  96. }
  97. #endif
  98.  
  99. #if MM_DEBUG
  100. //----------------------------------------------------------------------------------------
  101. // ODMemoryHook::AboutToAllocate
  102. //----------------------------------------------------------------------------------------
  103. #pragma segment HeapSeg
  104.  
  105. ODBlockSize ODMemoryHook::AboutToAllocate(ODBlockSize size) const
  106. {
  107.     return size;
  108. }
  109. #endif
  110.  
  111. #if MM_DEBUG
  112. //----------------------------------------------------------------------------------------
  113. // ODMemoryHook::DidAllocate
  114. //----------------------------------------------------------------------------------------
  115. #pragma segment HeapSeg
  116.  
  117. void *ODMemoryHook::DidAllocate(void* blk, ODBlockSize)
  118. {
  119.     return blk;
  120. }
  121. #endif
  122.  
  123. #if MM_DEBUG
  124. //----------------------------------------------------------------------------------------
  125. // ODMemoryHook::AboutToBlockSize
  126. //----------------------------------------------------------------------------------------
  127. #pragma segment HeapSeg
  128.  
  129. const void *ODMemoryHook::AboutToBlockSize(const void* blk)
  130. {
  131.     return blk;
  132. }
  133. #endif
  134.  
  135. #if MM_DEBUG
  136. //----------------------------------------------------------------------------------------
  137. // ODMemoryHook::AboutToFree
  138. //----------------------------------------------------------------------------------------
  139. #pragma segment HeapSeg
  140.  
  141. void *ODMemoryHook::AboutToFree(void* blk)
  142. {
  143.     return blk;
  144. }
  145. #endif
  146.  
  147. #if MM_DEBUG
  148. //----------------------------------------------------------------------------------------
  149. // ODMemoryHook::AboutToRealloc
  150. //----------------------------------------------------------------------------------------
  151. #pragma segment HeapSeg
  152.  
  153. void ODMemoryHook::AboutToRealloc(void* &, ODBlockSize &)
  154. {
  155. }
  156. #endif
  157.  
  158. #if MM_DEBUG
  159. //----------------------------------------------------------------------------------------
  160. // ODMemoryHook::DidRealloc
  161. //----------------------------------------------------------------------------------------
  162. #pragma segment HeapSeg
  163.  
  164. void *ODMemoryHook::DidRealloc(void *oldBlk, void *blk, ODBlockSize)
  165. {
  166.     return blk;
  167. }
  168. #endif
  169.  
  170. #if MM_DEBUG
  171. //----------------------------------------------------------------------------------------
  172. // ODMemoryHook::AboutToReset
  173. //----------------------------------------------------------------------------------------
  174. #pragma segment HeapSeg
  175.  
  176. void ODMemoryHook::AboutToReset()
  177. {
  178. }
  179. #endif
  180.  
  181. #if MM_DEBUG
  182. //----------------------------------------------------------------------------------------
  183. // ODMemoryHook::Comment
  184. //----------------------------------------------------------------------------------------
  185. #pragma segment HeapSeg
  186.  
  187. void ODMemoryHook::Comment(const char*)
  188. {
  189. }
  190. #endif
  191.  
  192. #if MM_DEBUG
  193. //----------------------------------------------------------------------------------------
  194. // ODMemoryHook::GetType
  195. //----------------------------------------------------------------------------------------
  196. #pragma segment HeapSeg
  197.  
  198. long ODMemoryHook::GetType() const
  199. {
  200.     return ODMemoryHook::kNoType;
  201. }
  202. #endif
  203.  
  204.  
  205. //========================================================================================
  206. // CLASS MemoryHookList (MM_DEBUG only)
  207. //========================================================================================
  208.  
  209. #if MM_DEBUG
  210. //----------------------------------------------------------------------------------------
  211. // MemoryHookList::MemoryHookList
  212. //----------------------------------------------------------------------------------------
  213. #pragma segment HeapSeg
  214.  
  215. MemoryHookList::MemoryHookList()
  216. {
  217. }
  218. #endif
  219.  
  220. #if MM_DEBUG
  221. //----------------------------------------------------------------------------------------
  222. // MemoryHookList::Add
  223. //----------------------------------------------------------------------------------------
  224. #pragma segment HeapSeg
  225.  
  226. void MemoryHookList::Add(ODMemoryHook* aMemoryHook)
  227. {
  228.     // Add at the fEnd of the list by adding after the last hook in the list.
  229.     
  230.     ODMemoryHook* afterHook = fHead.fPreviousHook;
  231.     
  232.     aMemoryHook->fNextHook = afterHook->fNextHook;
  233.     afterHook->fNextHook->fPreviousHook = aMemoryHook;
  234.     aMemoryHook->fPreviousHook = afterHook;
  235.     afterHook->fNextHook = aMemoryHook;
  236. }
  237. #endif
  238.  
  239. #if MM_DEBUG
  240. //----------------------------------------------------------------------------------------
  241. // MemoryHookList::Remove
  242. //----------------------------------------------------------------------------------------
  243. #pragma segment HeapSeg
  244.  
  245. void MemoryHookList::Remove(ODMemoryHook* aMemoryHook)
  246. {
  247.     aMemoryHook->fPreviousHook->fNextHook = aMemoryHook->fNextHook;
  248.     aMemoryHook->fNextHook->fPreviousHook = aMemoryHook->fPreviousHook;
  249. }
  250. #endif
  251.  
  252. #if MM_DEBUG
  253. //----------------------------------------------------------------------------------------
  254. // MemoryHookList::First
  255. //----------------------------------------------------------------------------------------
  256. #pragma segment HeapSeg
  257.  
  258. ODMemoryHook* MemoryHookList::First() const
  259. {
  260.     ODMemoryHook *h = fHead.fNextHook;
  261.     return h != &fHead ? h : NULL;
  262. }
  263. #endif
  264.  
  265. #if MM_DEBUG
  266. //----------------------------------------------------------------------------------------
  267. // MemoryHookList::Next
  268. //----------------------------------------------------------------------------------------
  269. #pragma segment HeapSeg
  270.  
  271. ODMemoryHook* MemoryHookList::After( ODMemoryHook *h ) const
  272. {
  273.     h = h->fNextHook;
  274.     return h != &fHead ? h : NULL;
  275. }
  276. #endif
  277.  
  278. #if MM_DEBUG
  279. //----------------------------------------------------------------------------------------
  280. // MemoryHookList::Previous
  281. //----------------------------------------------------------------------------------------
  282. #pragma segment HeapSeg
  283.  
  284. ODMemoryHook* MemoryHookList::Before( ODMemoryHook *h ) const
  285. {
  286.     h = h->fPreviousHook;
  287.     return h != &fHead ? h : NULL;
  288. }
  289. #endif
  290.  
  291. #if MM_DEBUG
  292. //----------------------------------------------------------------------------------------
  293. // MemoryHookList::Last
  294. //----------------------------------------------------------------------------------------
  295. #pragma segment HeapSeg
  296.  
  297. ODMemoryHook* MemoryHookList::Last() const
  298. {
  299.     ODMemoryHook *h = fHead.fPreviousHook;
  300.     return h != &fHead ? h : NULL;
  301. }
  302. #endif
  303.  
  304. #if MM_DEBUG
  305. //----------------------------------------------------------------------------------------
  306. // MemoryHookList::~MemoryHookList
  307. //----------------------------------------------------------------------------------------
  308. #pragma segment HeapSeg
  309.  
  310. MemoryHookList::~MemoryHookList()
  311. {
  312.     for (ODMemoryHook *hook = First(); hook != NULL; hook = First())
  313.     {
  314.         Remove(hook);
  315.         delete hook;
  316.     }
  317. }
  318. #endif
  319.  
  320.  
  321.